/*
 * @Author: your name
 * @Date: 2024-03-27 12:40:10
 * @LastEditTime: 2024-03-27 12:57:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\59.螺旋矩阵-ii.cpp
 */
/*
 * @lc app=leetcode.cn id=59 lang=cpp
 *
 * [59] 螺旋矩阵 II
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        // 遍历n/2圈
        int start_x = 0, start_y = 0, offset = 1,count = 1;
        int loop = n/2;
        vector<vector<int>> ans(n,vector<int>(n));
        while(loop--){
            for( ; start_y < n-offset; start_y++){
                ans[start_x][start_y] = count++;
            }
            for( ; start_x < n-offset; start_x++){
                ans[start_x][start_y] = count++;
            }
            for( ; start_y > offset-1; start_y--){
                ans[start_x][start_y] = count++;
            }
            for( ; start_x > offset-1; start_x--){
                ans[start_x][start_y] = count++;
            }
            start_x++;
            start_y++;
            offset++;
        }
        // 奇数
        if(n%2 == 1){
            ans[start_x][start_y] = count;
        }
        return ans;
    }
};
// @lc code=end

